package com.supermap.dataservice.common.dao;

import com.supermap.dubbo.api.common.entity.Crop;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:作物数据访问层
 * @Date:18/1/8 上午11:36
 */
@Repository
@Mapper
public interface CropDao {

    /**
     * 获得所有的作物信息
     * @return
     */
    @Select("select * from t_crop where level=#{level}")
    public List<Crop> getAllCropByLevels(@Param("level") int level);

    /**
     * 获得所有的作物信息的等级信息
     * @return
     */
    @Select("select * from t_crop")
    public List<Crop> getAllCrop();

    /**
     * 插入作物信息
     * @param crop
     */
    @Insert("insert t_crop(id,code,name,description,level,parentid,img_ico) values("
            + "#{Crop.id},#{Crop.code},#{Crop.name},"
            + "#{Crop.description},#{Crop.level},#{Crop.parentid},#{Crop.img_ico})")
    public void saveCrop(@Param("Crop") Crop crop);

    /**
     * 更新作物信息
     * @param crop
     */
    @Update("update t_crop set code=#{Crop.code},"
            + "name=#{Crop.name},description=#{Crop.description},level=#{Crop.level},"
            + "parentid=#{Crop.parentid},img_ico=#{Crop.img_ico} where id=#{Crop.id}")
    public void updateCrop(@Param("Crop") Crop crop);

    /**
     * 删除作物信息
     * @param id
     */
    @Delete("delete from t_crop where id=#{id} ")
    public void deleteCrop(@Param("id") String id);

    /**
     * 增加作物定制
     * @param userId
     * @param cropCode
     */
    @Insert("Insert into t_user_crop(userid,crop)values(#{userId},#{cropCode})")
    public void addUserCrop(@Param("userId")String userId,@Param("cropCode")String cropCode);

    /**
     * 删除作物定制
     * @param userId
     * @param cropCode
     */
    @Delete("delete from t_user_crop where userid=#{userId} and crop=#{cropCode}")
    public void deleteUserCrop(@Param("userId")String userId,@Param("cropCode")String cropCode);

    /**
     * 获取作物定制情况
     * @param userId
     */
    @Select("SELECT\n" +
            "\tt1.*,\n" +
            "IF(isnull(t2.id) , 0 , 1) AS flag\n" +
            "FROM\n" +
            "\tt_crop t1\n" +
            "LEFT JOIN t_user_crop t2 ON t1.`code` = t2.crop and t2.userid=#{userId}\n" +
            "WHERE\n" +
            "\tt1.`level` = 1 ")
    public List<Crop> getUserCropByUserId(@Param("userId")String userId);

}
